package com.sanpu.kxe.business.main.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.PushMaterialOutput;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：地推物料
 * Created by 薛勇军 on 2018/8/20.
 */

public class PushMaterialAdapter extends BaseAdapter {

    private Context mContext;
    private List<PushMaterialOutput> dataList = new ArrayList<>();

    public PushMaterialAdapter(Context mContext, List<PushMaterialOutput> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
    }

    public void setNotifyData(List<PushMaterialOutput> dataList) {
        this.dataList = dataList;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (dataList == null)
            return 0;
        return dataList.size();
    }

    @Override
    public PushMaterialOutput getItem(int position) {
        if (dataList == null)
            return null;
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_push_material, null);
        }
        ImageView materialIv = ButterKnife.findById(convertView, R.id.materialIv);
        TextView materialTv = ButterKnife.findById(convertView, R.id.materialTv);

        PushMaterialOutput retDatBean = getItem(position);
        if (retDatBean != null) {
            JKImageLoader.getInstance().displayImage(mContext,
                    UrlConstants.getBaseUrl()+retDatBean.getPicAdd(), materialIv);
            if (!TextUtils.isEmpty(retDatBean.getName()))
                materialTv.setText(retDatBean.getName());
        }
        return convertView;
    }
}
